<?php

if ( ! class_exists("MyLotto_3d") )
{
    include_once( "class_3d/MyLotto_3d.php" );
}

class MyLotto_5h extends MyLotto_3d
{

    public function copyRateZeroToN($lotto_3d_id)
    {
        global $g_gametype_3d;
        $r21Str = $this->gametype == 20?",r21":"";
        $table_name = "rate_".$g_gametype_3d[$this->gametype]['en'];
        $sql = "INSERT INTO {$table_name} (lotto_id, bettype, xyid , subid, disk, r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20 {$r21Str})";
        $sql .= " SELECT {$lotto_3d_id} AS lotto_id, bettype, xyid , subid, disk, r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20 {$r21Str}";
        $sql .= " FROM {$table_name} WHERE lotto_id=0";
		return $this->db->query($sql);
    }

    public function checkBS($val , $flagVal = 10 , $zNum = 21)
    {
        if ( $val == $zNum )
        {
            return 0;
        }
        if ( $flagVal < $val )
        {
            return 1;
        }
        return 2;
    }

    public function checkBSTail($val , $zNum = 21)
    {
        if ( $val == 21 )
        {
            return 0;
        }
        $valTail = substr($val , strlen($val) - 1 , 1);
        return $this->checkBS($valTail , 4 , 21);
    }

    public function checkSD($val , $zNum = 21)
    {
        if ( $val == $zNum )
        {
            return 0;
        }
        if ( $val == 0 )
        {
            return 0;
        }
        if ( $val % 2 == 0 )
        {
            return 2;
        }
        return 1;
    }

    public function checkAddSD($val , $zNum = 21)
    {
        if ( $val == $zNum )
        {
            return 0;
        }
        $valArr = str_split($val);
        $total = 0;
        $l = count($valArr);
        $i = 0;
        while ( $i < $l )
        {
            $total += $valArr[$i];
            ++ $i;
        }
        return $this->checkSD($total , 9999999);
    }

    public function checkCLR($val)
    {
        $clrN = $val % 3;
        return $clrN?$clrN:3;
    }

    public function checkCFP($val)
    {
        $sub = $val % 7?0:1;
        return min(intval($val / 7) - $sub + 1 , 3);
    }

    public function checkFH($val , $zNum = 21)
    {
        if ( $val == $zNum )
        {
            return 0;
        }
        $sub = $val % 5?0:1;
        return min(intval($val / 5) - $sub + 1 , 4);
    }

    public function checkPST($val)
    {
        $clrN = $val % 4;
        return $clrN?$clrN:4;
    }

    public function checkOpen($n3 , $n2 , $n1 , $n4 , $n5 , $n6 = -1 , $n7 = -1 , $n8 = -1)
    {
        if ( $this->_8NFlag )
        {
            $okArr = array ( $n3 , $n2 , $n1 , $n4 , $n5 , $n6 , $n7 , $n8 );
            $chkArr = array_unique($okArr);
            if ( count($okArr) != count($chkArr) )
            {
                return false;
            }
            $chkArr2 = array ( 1 => 1 , 2 => 2 , 3 => 3 , 4 => 4 , 5 => 5 , 6 => 6 , 7 => 7 , 8 => 8 , 9 => 9 , 10 => 10 , 11 => 11 , 12 => 12 , 13 => 13 , 14 => 14 , 15 => 15 , 16 => 16 , 17 => 17 , 18 => 18 , 19 => 19 , 20 => 20 );
            $i = 0;
            while ( $i < 8 )
            {
                if ( ! isset($chkArr2[$chkArr[$i]]) )
                {
                    return false;
                }
                ++ $i;
            }
            return true;
        }
        $okArr = array ( $n3 , $n2 , $n1 , $n4 , $n5 );
        $chkArr = array_unique($okArr);
        if ( count($okArr) != count($chkArr) )
        {
            return false;
        }
        $chkArr = array ( 1 => 1 , 2 => 2 , 3 => 3 , 4 => 4 , 5 => 5 , 6 => 6 , 7 => 7 , 8 => 8 , 9 => 9 , 10 => 10 , 11 => 11 , 12 => 12 , 13 => 13 , 14 => 14 , 15 => 15 , 16 => 16 , 17 => 17 , 18 => 18 , 19 => 19 , 20 => 20 , 21 => 21 );
        if ( isset($chkArr[$n5] , $chkArr[$n4]) && isset($chkArr[$n3]) && isset($chkArr[$n2]) && isset($chkArr[$n1]) )
        {
            return true;
        }
        return false;
    }

    public function checkLT($n1 , $n8)
    {
        if ( $n8 < $n1 )
        {
            return "1";
        }
        return "2";
    }

    public function checkNum($n)
    {
        if ( $n == "" )
        {
            return true;
        }
        if ( 0 - 1 <= $n && $n < 22 )
        {
            return true;
        }
        return false;
    }

    public function showText($n , $type , $code_5 = true)
    {
        $conf = new Config();
        $_5hxyMode = intval($conf->Get("5hxyMode"));
        $_5hx21Mode = intval($conf->Get("5hx21Mode"));
        $index = str_split($n);
        if ( $_5hxyMode == 0 )
        {
            if ( $_5hx21Mode == 0 )
            {
                $result = "<font style=\"font-weight:normal; color:black;\">和</font>";
            }
            else
            {
                $result = "<font style=\"font-weight:normal; color:black;\">输</font>";
            }
        }
        else
        {
            $result = "<font style=\"font-weight:bold; color:black;\">21</font>";
        }
        $sdArr = array ( 0 => $result , 1 => "<font class=text01>单</font>" , 2 => "<font class=text02>双</font>" );
        $bsArr = array ( 0 => $result , 1 => "<font class=text01>大</font>" , 2 => "<font class=text02>小</font>" );
        $color_Arr = array ( 1 => "<font color=\"red\">红</font>" , 2 => "<font color=\"blue\">蓝</font>" , 3 => "<font color=\"green\">绿</font>" );
        $four_Arr = array ( 0 => $result , 1 => "<font color=\"saddlebrown\">福</font>" , 2 => "<font color=\"dimgray\">禄</font>" , 3 => "<font color=\"darkorange\">寿</font>" , 4 => "<font color=\"5200A0\">喜</font>" );
        $position_Arr = array ( 1 => "<font color=\"saddlebrown\">东</font>" , 2 => "<font color=\"dimgray\">南</font>" , 3 => "<font color=\"darkorange\">西</font>" , 4 => "<font color=\"5200A0\">北</font>" );
        $three_Arr = array ( 1 => "<font color=\"red\">中</font>" , 2 => "<font color=\"blue\">发</font>" , 3 => "<font color=\"green\">白</font>" );
        $total_sdArr = array ( 0 => "<font style=\"font-weight:bold; color:black;\">55</font>" , 1 => "<font class=text01>单</font>" , 2 => "<font class=text02>双</font>" );
        $total_bsArr = array ( 0 => "<font style=\"font-weight:bold; color:black;\">55</font>" , 1 => "<font class=text01>大</font>" , 2 => "<font class=text02>小</font>" );
        $animal = array ( 1 => "<font style=\"color:black;\">龙</font>" , 2 => "<font style=\"color:red;\">虎</font>" );
        $total_tbsArr = array ( 0 => "<font style=\"font-weight:bold; color:black;\">和</font>" , 1 => "<font class=text01>大</font>" , 2 => "<font class=text02>小</font>" );
        $reVal = "";
        switch ( $type )
        {
            case "sd" :
                $reVal = $sdArr[$index[0]];
                break;
            case "bs" :
                $reVal = $bsArr[$index[1]];
                break;
            case "t_bs" :
                $reVal = $bsArr[$index[2]];
                break;
            case "s_sd" :
                $reVal = $sdArr[$index[3]];
                break;
            case "color" :
                $reVal = $color_Arr[$index[4]];
                break;
            case "four" :
                $reVal = $four_Arr[$index[5]];
                break;
            case "total_sd" :
                $reVal = $total_sdArr[$index[0]];
                break;
            case "total_bs" :
                $reVal = $total_bsArr[$index[1]];
                break;
            case "total_8hbs" :
                $reVal = $bsArr[$index[2]];
                break;
            case "three" :
                $reVal = $three_Arr[$index[4]];
                break;
            case "position" :
                $reVal = $position_Arr[$index[5]];
                break;
            case "animal" :
                $reVal = $animal[$index[3]];
                break;
            case "total_tbsArr" :
                $reVal = $total_tbsArr[$index[1]];
                break;
            default :
                $reVal = $n;
                break;
        }
        return $reVal;
    }

    public function calOpenNum($n3 , $n2 , $n1 , $n4 , $n5 , $n6 = -1 , $n7 = -1 , $n8 = -1 , $n9 = -1 , $n10 = -1)
    {
        if ( $this->_8NFlag )
        {
            return $this->calOpenNum8N($n3 , $n2 , $n1 , $n4 , $n5 , $n6 , $n7 , $n8);
        }
        return $this->calOpenNum5N($n3 , $n2 , $n1 , $n4 , $n5 , $n6 , $n7 , $n8 , $n9 , $n10);
    }

    public function calOpenNum8N($n3 , $n2 , $n1 , $n4 , $n5 , $n6 , $n7 , $n8)
    {
        $updateData = array ( );
        if ( $this->checkOpen($n3 , $n2 , $n1 , $n4 , $n5 , $n6 , $n7 , $n8) )
        {
            $nArr = array ( 1 => $n1 , 2 => $n2 , 3 => $n3 , 4 => $n4 , 5 => $n5 , 6 => $n6 , 7 => $n7 , 8 => $n8 );
            foreach ( $nArr as $nArrKey => $nArrVal )
            {
                $nStr = "";
                $nStr .= $this->checkSD($nArrVal , 999);
                $nStr .= $this->checkBS($nArrVal , 10 , 999);
                $nStr .= $this->checkBSTail($nArrVal , 999);
                $nStr .= $this->checkAddSD($nArrVal , 999);
                $nStr .= $this->checkCFP($nArrVal);
                $nStr .= $this->checkPST($nArrVal);
                $updateData["p{$nArrKey}"] = strlen($nStr) == 6?$nStr:"000000";
            }
            $total = $n1 + $n2 + $n3 + $n4 + $n5 + $n6 + $n7 + $n8;
            $nStr = "";
            $nStr .= $this->checkSD($total , 999);
            $nStr .= $this->checkBS($total , 84 , 84);
            $nStr .= $this->checkBS(substr($total , strlen($total) - 1 , 1) , 4 , 999);
            $nStr .= $this->checkLT($n1 , $n8);
            $updateData['p9'] = strlen($nStr) == 4?$nStr:"0000";
        }
        return $updateData;
    }

    public function calOpenNum5N($n3 , $n2 , $n1 , $n4 , $n5 , $n6 = -1 , $n7 = -1 , $n8 = -1 , $n9 = -1 , $n10 = -1)
    {
        $updateData = array ( );
        if ( $this->checkOpen($n3 , $n2 , $n1 , $n4 , $n5) )
        {
            if ( $this->gametype == 24 )
            {
                $nArr = array ( 1 => $n1 , 2 => $n2 , 3 => $n3 , 4 => $n4 , 5 => $n5 , 6 => $n6 , 7 => $n7 , 8 => $n8 , 9 => $n9 , 10 => $n10 );
                foreach ( $nArr as $nArrKey => $nArrVal )
                {
                    $nStr = "";
                    $nStr .= $this->checkSD($nArrVal , 999);
                    $nStr .= $this->checkBS($nArrVal , 5 , 999);
                    $nStr .= "0";
                    if ( $nArrKey <= 5 )
                    {
                        if ( $nArrVal > $nArr[(10 - $nArrKey + 1)] )
                        {
                            $nStr .= "1";
                        }
                        else
                        {
                            $nStr .= "2";
                        }
                    }
                    else
                    {
                        $nStr .= "0";
                    }
                    $nStr .= "0";
                    $nStr .= "0";
                    $updateData["p{$nArrKey}"] = strlen($nStr) == 6?$nStr:"000000";
                    $total = $n1 + $n2;
                    $nStr = "";
                    $nStr .= $this->checkSD($total , 999);
                    $nStr .= $this->checkBS($total , 11 , 999);
                    $updateData['p11'] = $nStr;
                }
            }
            else
            {
                $nArr = array ( 1 => $n1 , 2 => $n2 , 3 => $n3 , 4 => $n4 , 5 => $n5 );
                foreach ( $nArr as $nArrKey => $nArrVal )
                {
                    $nStr = "";
                    $nStr .= $this->checkSD($nArrVal , 21);
                    $nStr .= $this->checkBS($nArrVal , 10 , 21);
                    $nStr .= $this->checkBSTail($nArrVal , 21);
                    $nStr .= $this->checkAddSD($nArrVal , 21);
                    $nStr .= $this->checkCLR($nArrVal);
                    $nStr .= $this->checkFH($nArrVal , 21);
                    $updateData["p{$nArrKey}"] = strlen($nStr) == 6?$nStr:"000000";
                }
                $total = $n1 + $n2 + $n3 + $n4 + $n5;
                if ( $total == 55 )
                {
                    $updateData['p6'] = "00";
                }
                else
                {
                    $nStr = "";
                    $nStr .= $this->checkSD($total , 55);
                    $nStr .= $this->checkBS($total , 54 , 99999);
                    $updateData['p6'] = $nStr;
                }
            }
        }
        return $updateData;
    }

    public function getLotto5HByDateTime()
    {
        if ( $this->gametype == 24 )
        {
            $sql = "SELECT * FROM {$this->table_name} WHERE ('".date("Y-m-d H:i:s" , systime())."' BETWEEN startbet AND stopbet) AND ((n10 + n9 + n8 + n7 + n6 + n5 + n4 + n3 + n2 + n1) = -10) limit 0,1";
        }
        else
        {
            $sql = "SELECT * FROM {$this->table_name} WHERE ('".date("Y-m-d H:i:s" , systime())."' BETWEEN startbet AND stopbet) AND ((n5 + n4 + n3 + n2 + n1) = -5) limit 0,1";
        }
        $result = $this->db->query($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

    /* 下一期时间 */

    public function getLottoNextDateTime()
    {
        $n = "";
        if ( $this->gametype == 24 )
        {
            $n = "((n10 + n9 + n8 + n7 + n6 + n5 + n4 + n3 + n2 + n1) = -10)";
        }
        else
        {
            $n = "((n5 + n4 + n3 + n2 + n1) = -5)";
        }
        $sql = "SELECT time_to_sec(timediff(startbet,sysdate())) as sec FROM {$this->table_name} WHERE startbet > sysdate() AND {$n} ORDER BY startbet ASC limit 0,1";
		$result = $this->db->query($sql);
        unset($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

}